<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>promise</title>
</head>
<body>
promise
</body>
</html>
<script src="javascripts/axios.min.js"></script>
<script>
    /*
    var p1 = Promise.resolve(3);
    var p2 = 1337;
    var p3 = new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve("foo")
        }, 100)
    });
    Promise.all([p1, p2, p3]).then(function (values) {
        console.log(values);
    });
    */
    //todo  - > [3, 1337, "foo"]
    //----------------------------------------------------------//
    /*
    var f1 = new Promise(function (resolve, reject) {
        setTimeout(function () {
            //console.log("f1");
            resolve("f1");
        }, 1000)
    });
    var f2 = new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve("f2");
        }, 1000)
    })
    Promise.all([f1, f2]).then(function (value) {
        console.log(value);
    })
    */
    //todo  - > ["f1", "f2"]
    //----------------------------------------------------------//
    //todo -- > 每个请求的参数,都依赖上一次请求返回的结果
    /*
    var p1 = new Promise(function (resolve, reject) {
        axios.post('/testPromise', {data: {a: 1}}).then(function (res) {
            resolve(res);
        })
    });
    var p2 = new Promise(function (resolve, reject) {
        p1.then(function (res) {
            axios.post('/testPromise', {data: {a: res.data.a}}).then(function (res) {
                resolve(res);
            })
        })
    });
    var p3 = new Promise(function (resolve, reject) {
        p2.then(function (res) {
            axios.post('/testPromise', {data: {a: res.data.a}}).then(function (res) {
                resolve(res);
            })
        });
    });
    Promise.all([p1, p2, p3]).then(function (values) {
        console.log(values);
    });
    p3.then(function (res) {
        console.log(res.data.a);
    })

    */
    var p1 = new Promise(function (resolve, reject) {
        var i = 0;
        var t = setInterval(function () {
            console.log(i);
            i++;
            if (i == 5) {
                resolve(i);
                clearInterval(t);
            }
        }, 100)
    });
    /*p1.then(function (res) {
        console.log('res --- >', res);
    });*/
    var p2 = new Promise(function (resolve, reject) {
        p1.then(function (p1value) {
            var p2value = p1value;
            var t = setInterval(function () {
                console.log(p2value);
                p2value++;
                if (p2value == 10) {
                    resolve(p2value);
                    clearInterval(t);
                }
            }, 100)
        });
    });
    Promise.all([p1, p2]).then(function (value) {
        console.log('all value ---> ', value);
    });
</script>